package com.mercenary.wein.screenControl.ui;

import com.mercenary.wein.screenControl.ui.listener.ScreenCaptureListener;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;

public class JPanelScreen extends JPanel implements ScreenCaptureListener {

    public float coef = 1;
    double origX;
    double origY;
    boolean landscape;

    Dimension size = null;
    Image image = null;
    JPanel J2 = new JPanel();
    JPanel J3 = new JPanel();
    JCheckBox j3 = new JCheckBox();
    JTextField name = new JTextField(8);
    String serialNo;
    JTextField groupname = new JTextField(5);
    JMenuBar menu = new JMenuBar();

    public String getSerialNo() {
        return serialNo;
    }

    public void setSerialNo(String serialNo) {
        this.serialNo = serialNo;
    }

    public JPanelScreen(ActionListener getControlListener, ActionListener removeControlListener, ActionListener pressHomeListener, ActionListener pressBackListener) {
        this.setFocusable(true);
        JMenuItem getControl = new JMenuItem("控制");
        if (getControlListener != null) {
            getControl.addActionListener(getControlListener);
        }
        JMenuItem removeControl = new JMenuItem("取消");
        if (removeControlListener != null) {
            removeControl.addActionListener(removeControlListener);
        }
        JMenuItem pressHome = new JMenuItem("HOME");
        if (getControlListener != null) {
            pressHome.addActionListener(pressHomeListener);
        }
        JMenuItem pressBack = new JMenuItem("BACK");
        if (removeControlListener != null) {
            pressBack.addActionListener(pressBackListener);
        }
        menu.add(getControl);
        menu.add(removeControl);
        menu.add(pressHome);
        menu.add(pressBack);
        this.add(menu);
        J2.setPreferredSize(new Dimension(270, 50));//设置大小
        J2.setBackground(Color.white);
        J2.setLayout(new FlowLayout(FlowLayout.LEADING));
        J2.add(j3);
        J2.add(name);
        J2.add(groupname);
        this.setLayout(new BorderLayout());
        this.add(J2, BorderLayout.NORTH);
        this.add(menu, BorderLayout.SOUTH);
        //监听事件
        j3.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if(j3.isSelected()){
                    MainFrame.choosedDevice.add(getSerialNo());
                }else {
                    MainFrame.choosedDevice.remove(getSerialNo());
                }
            }
        });

    }

    public void refreshData(String serialNo, String dName,String gName){
        setSerialNo(serialNo);
        name.setText(dName);
        groupname.setText(gName);
        name.repaint();
        groupname.repaint();
    }


    @Override
    public void handleNewImage(Dimension size, BufferedImage image, boolean landscape) {
        this.landscape = landscape;
        this.size = size;
        this.image = image;
        repaint();
    }

    protected void paintComponent(Graphics g) {
        if (size == null)
            return;
        if (size.height == 0)
            return;
        g.clearRect(0, 0, getWidth(), getHeight());
        double width = Math.min(getWidth(), size.width * getHeight() / size.height);
        coef = (float) width / (float) size.width;
        double height = width * size.height / size.width;
        origX = (getWidth() - width) / 2;
        origY = (getHeight() - height) / 2;
        g.drawImage(image, (int) origX, (int) origY, (int) width, (int) height, this);
    }


    public Point getRawPoint(Point p1) {
        Point p2 = new Point();
        p2.x = (int) ((p1.x - origX) / coef);
        p2.y = (int) ((p1.y - origY) / coef);
        return p2;
    }


}
